import {digit} from './number'

/**
 * 倒计时
 * @param endTime 结束时间
 * @param serverTime 服务端当前时间
 * @param callback 回调
 * @returns {number} 定时器实例
 */
export function countdown(endTime, serverTime, callback) {
  let type = typeof serverTime === 'function',
    end = new Date(endTime).getTime(),
    now = new Date((!serverTime || type) ? new Date().getTime() : serverTime).getTime(),
    count = end - now,
    time = [
      Math.floor(count / (1000 * 60 * 60 * 24)),  // 天
      Math.floor(count / (1000 * 60 * 60)) % 24,  // 时
      Math.floor(count / (1000 * 60)) % 60,  // 分
      Math.floor(count / 1000) % 60  // 秒
    ];
  if (type) {
    callback = serverTime;
  }
  let timer = setTimeout(() => {
    this.countdown(endTime, now + 1000, callback);
  }, 1000);
  callback && callback(count > 0 ? time : [0, 0, 0, 0], serverTime, timer);
  if (count <= 0) {
    clearTimeout(timer);
  }
  return timer;
}



/**
 * 时间格式化
 * @param time 时间
 * @param format 格式
 * @returns {string}
 */
export function toDateString(time, format) {
  if (!time) {
    return '';
  }
  if (typeof time === 'string') {
    time = time.replace(/-/g, '/');
  } else if (typeof time === 'number' && String(time).length === 10) {
    time = time * 1000;  // 10位时间戳处理
  }
  const date = new Date(time),
    ymd = [
      digit(date.getFullYear(), 4),
      digit(date.getMonth() + 1),
      digit(date.getDate())
    ],
    hms = [
      digit(date.getHours()),
      digit(date.getMinutes()),
      digit(date.getSeconds())
    ];
  return (format || 'yyyy-MM-dd HH:mm:ss')
    .replace(/yyyy/g, ymd[0])
    .replace(/MM/g, ymd[1])
    .replace(/dd/g, ymd[2])
    .replace(/HH/g, hms[0])
    .replace(/mm/g, hms[1])
    .replace(/ss/g, hms[2]);
}

/**
 * 秒转成 时:分:秒
 * @param time
 * @param s
 * @returns {string}
 */
export function toSecondFormat (time, s = false) {
  let hour = Math.floor(time / 3600)
  let minute = Math.floor(time % 3600 / 60)
  hour = (hour < 10) ? '0' + hour : hour
  minute = (minute < 10) ? '0' + minute : minute
  if (s) {
    let second = Math.floor((time % 3600) % 60)
    second = (second < 10) ? '0' + second : second
    return hour + ':' + minute + ':' + second
  }
  return hour + ':' + minute
}

/**
 * 时间语义化
 * @param time 时间
 * @param onlyDate 超过30天是否仅返回日期
 * @returns {string}
 */
export function timeAgo(time, onlyDate) {
  if (!time) {
    return '';
  }
  if (typeof time === 'string') {
    time = time.replace(/-/g, '/');
  }
  let arr = [[], []],
    stamp = new Date().getTime() - new Date(time).getTime();
  // 30天以上返回具体日期
  if (stamp > 1000 * 60 * 60 * 24 * 31) {
    stamp = new Date(time);
    arr[0][0] = digit(stamp.getFullYear(), 4);
    arr[0][1] = digit(stamp.getMonth() + 1);
    arr[0][2] = digit(stamp.getDate());
    // 是否输出时间
    if (!onlyDate) {
      arr[1][0] = digit(stamp.getHours());
      arr[1][1] = digit(stamp.getMinutes());
      arr[1][2] = digit(stamp.getSeconds());
    }
    return arr[0].join('-') + ' ' + arr[1].join(':');
  }
  if (stamp >= 1000 * 60 * 60 * 24) {
    return ((stamp / 1000 / 60 / 60 / 24) | 0) + '天前';
  } else if (stamp >= 1000 * 60 * 60) {
    return ((stamp / 1000 / 60 / 60) | 0) + '小时前';
  } else if (stamp >= 1000 * 60 * 3) {
    return ((stamp / 1000 / 60) | 0) + '分钟前';
  } else if (stamp < 0) {
    return '未来';
  } else {
    return '刚刚';
  }
}

/**
 * 日期转为周
 * @param timestamp
 * @returns {string}
 */
export function toWeek(timestamp) {
  let weekDay = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
  let date = new Date(timestamp * 1000)
  let day = date.getDay()
  return weekDay[day];
}
